library(Seurat)
library(stringr)
library(tibble)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(patchwork)
se_path <- c("Gg_D05_ctrl_seurat_070323",
             "Gg_D07_ctrl_seurat_070323",
             "Gg_ctrl_1_seurat_070323",
             "Gg_ctrl_2_seurat_070323",
             "Gg_ctrl_int_seurat_250723",
             "Gg_lumb_1_seurat_070323",
             "Gg_lumb_2_seurat_070323",
             "Gg_lumb_int_seurat_250723",
             "Gg_poly_1_seurat_070323",
             "Gg_poly_2_seurat_070323",
             "Gg_poly_int_seurat_250723",
             "Gg_ctrl_lumb_int_seurat_250723",
             "Gg_ctrl_poly_int_seurat_250723",
             "Gg_all_int_seurat_270524")
clust_col <- read.csv("~/spinal_cord_paper/annotations/broad_cluster_marker_colors.csv")
tsne_plots <- list()
fine_tsne_plots <- list()
cell_numbers <- list()

for (i in seq(se_path)) {
  # load the data sets
  my.se <- readRDS(paste0("~/spinal_cord_paper/data/", se_path[i], ".rds"))
  annot <- read.csv(list.files("~/spinal_cord_paper/annotations",
                               pattern = str_remove(se_path[i], "_seurat_\\d{6}"),
                               full.names = TRUE))
  
  if(length(table(annot$number)) != length(table(my.se$seurat_clusters))) {
     stop("Number of clusters must be identical!")
  }
  
  my.se 
  
  # rename for left join
  annot <- annot %>% 
    mutate(fine = paste(fine, number, sep = "_")) %>% 
    mutate(number = factor(number, levels = 1:nrow(annot))) %>% 
    rename(seurat_clusters = number) 
    
  # add cluster annotation to meta data
  my.se@meta.data <- my.se@meta.data %>% 
    rownames_to_column("rowname") %>% 
    left_join(annot, by = "seurat_clusters") %>% 
    column_to_rownames("rowname")
  
  # colors to plot
  broad_cols <- clust_col %>% 
    filter(broad_cluster %in% annot$broad) %>% 
    pull(color)
  
  # plot
  tsne_plots[[i]] <- DimPlot(
    object = my.se,
    reduction = "tsne",
    group.by = "broad",
    cols = broad_cols,
    label = TRUE,
    label.size = 5
    ) +
    ggtitle(NULL)+
    theme_void() +
    NoLegend() +
    annotate(
      "text",
      label = paste0("broad ", se_path[i]),
      x = 0,
      y = 0,
      angle = 45
    )
  
  #fine clustering plots
  fine_tsne_plots[[i]] <- DimPlot(
    object = my.se,
    reduction = "tsne",
    group.by = "seurat_clusters",
    cols = rainbow(length(table(my.se$seurat_clusters))),
    label = TRUE,
    label.size = 3
    ) +
    ggtitle(NULL)+
    theme_void() +
    NoLegend() +
    annotate(
      "text",
      label = paste0("fine ", se_path[i]),
      x = 0,
      y = 0,
      angle = 45
    )
  
  # prepare bar plot
  cell_numbers[[i]] <- data.frame(table(my.se$broad),
                                  sample = factor(my.se@project.name)) %>%
    dplyr::mutate(sample_size = sum(Freq))

}

rm(my.se, annot)

names(tsne_plots) <- str_remove_all(se_path, "_seurat_\\d{6}")
names(cell_numbers) <- str_remove_all(se_path, "_seurat_\\d{6}")

tmp <- do.call(rbind, cell_numbers) %>%
  mutate(rel = Freq/sample_size) %>%
      rev()

tmp$Var1 <- factor(
  tmp$Var1,
  levels =
    c("progenitors",
      "FP",
      "RP",
      "FP/RP",
      "neurons",
      "OPC",
      "MFOL",
      "pericytes",
      "microglia",
      "blood",
      "vasculature"
      )
)
order_col <- match(levels(tmp$Var1), clust_col$broad_cluster)

bar <- ggplot(tmp, aes(
    x = sample,
    y = rel,
    fill = factor(Var1, levels = rev(levels(Var1)))
  )) +
  geom_bar(stat = "identity",
           position = "stack") +
  scale_fill_manual(values = rev(clust_col$color[order_col])) +
  labs(fill = "Cell types") +
  ggtitle("relative cell numbers of broad clusters")

bar

ggsave(
  filename = "~/spinal_cord_paper/figures/bar_plots.pdf",
  width = 14,
  plot = bar,
  device = "pdf"
)
Saving 14 x 7 in image
grid.arrange(grobs = tsne_plots)

ggsave(
   filename = "~/spinal_cord_paper/figures/tsne_plots.pdf",
   width = 7, height = 7,
   plot = marrangeGrob(tsne_plots, nrow=1, ncol=1, top = NULL)
)
grid.arrange(grobs = fine_tsne_plots)

ggsave(
   filename = "~/spinal_cord_paper/figures/fine_tsne_plots.pdf",
   width = 7, height = 7,
   plot = marrangeGrob(fine_tsne_plots, nrow=1, ncol=1, top = NULL)
)

Supplementary figure 3

Code to plot broad and fine clusters of all individual non integrated data sets. Exports raster to save space.

for (i in seq(tsne_plots)) {
  tsne_plots[[i]]$layers[[3]] <- NULL # remove annot text 
  tsne_plots[[i]]$layers[[2]] <- NULL # remove labels
  fine_tsne_plots[[i]]$layers[[3]] <- NULL # remove annot text
}

pdf("~/spinal_cord_paper/figures/Supplementary_Figure_3.pdf", width = 10, height = 10)
tsne_plots[[1]] + tsne_plots[[2]] + tsne_plots[[3]] + tsne_plots[[4]] +
  tsne_plots[[6]] + tsne_plots[[7]] + tsne_plots[[9]] + tsne_plots[[10]] +
  fine_tsne_plots[[1]] + fine_tsne_plots[[2]] + fine_tsne_plots[[3]] + fine_tsne_plots[[4]] +
  fine_tsne_plots[[6]] + fine_tsne_plots[[7]] + fine_tsne_plots[[9]] + fine_tsne_plots[[10]] + 
  plot_layout(ncol = 4, byrow = TRUE) +
  plot_annotation(tag_levels = 'A')
dev.off()
png 
  2 
# Date and time of Rendering
Sys.time()
[1] "2024-05-28 11:26:46 CEST"
sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /scicore/soft/apps/OpenBLAS/0.3.1-GCC-7.3.0-2.30/lib/libopenblas_sandybridgep-r0.3.1.so

locale:
[1] en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] patchwork_1.1.1    gridExtra_2.3      ggplot2_3.3.3      dplyr_1.0.10      
[5] tibble_3.1.8       stringr_1.4.0      SeuratObject_4.0.2 Seurat_4.0.5      

loaded via a namespace (and not attached):
  [1] Rtsne_0.15            colorspace_2.0-1      deldir_1.0-6         
  [4] ellipsis_0.3.2        ggridges_0.5.3        spatstat.data_3.0-0  
  [7] farver_2.1.0          leiden_0.3.9          listenv_0.8.0        
 [10] ggrepel_0.9.1         fansi_0.5.0           codetools_0.2-18     
 [13] splines_4.1.0         knitr_1.41            polyclip_1.10-0      
 [16] jsonlite_1.7.2        ica_1.0-2             cluster_2.1.2        
 [19] png_0.1-7             uwot_0.1.10           shiny_1.6.0          
 [22] sctransform_0.3.3     spatstat.sparse_3.0-0 compiler_4.1.0       
 [25] httr_1.4.2            assertthat_0.2.1      Matrix_1.3-3         
 [28] fastmap_1.1.0         lazyeval_0.2.2        cli_3.4.1            
 [31] later_1.2.0           htmltools_0.5.1.1     tools_4.1.0          
 [34] igraph_1.2.6          gtable_0.3.0          glue_1.6.2           
 [37] RANN_2.6.1            reshape2_1.4.4        Rcpp_1.0.7           
 [40] scattermore_0.7       jquerylib_0.1.4       vctrs_0.5.1          
 [43] nlme_3.1-152          lmtest_0.9-38         xfun_0.34            
 [46] globals_0.16.2        mime_0.10             miniUI_0.1.1.1       
 [49] lifecycle_1.0.3       irlba_2.3.3           goftest_1.2-2        
 [52] future_1.30.0         MASS_7.3-54           zoo_1.8-9            
 [55] scales_1.1.1          spatstat.core_2.1-2   promises_1.2.0.1     
 [58] spatstat.utils_3.0-1  parallel_4.1.0        RColorBrewer_1.1-2   
 [61] yaml_2.2.1            reticulate_1.22       pbapply_1.4-3        
 [64] sass_0.4.0            rpart_4.1-15          stringi_1.6.2        
 [67] highr_0.9             rlang_1.0.6           pkgconfig_2.0.3      
 [70] matrixStats_0.58.0    evaluate_0.20         lattice_0.20-44      
 [73] ROCR_1.0-11           purrr_0.3.4           tensor_1.5           
 [76] labeling_0.4.2        htmlwidgets_1.5.3     cowplot_1.1.1        
 [79] tidyselect_1.2.0      parallelly_1.33.0     RcppAnnoy_0.0.19     
 [82] plyr_1.8.6            magrittr_2.0.1        R6_2.5.0             
 [85] generics_0.1.3        DBI_1.1.1             withr_2.4.2          
 [88] pillar_1.8.1          mgcv_1.8-35           fitdistrplus_1.1-6   
 [91] survival_3.2-11       abind_1.4-5           sp_1.4-5             
 [94] future.apply_1.7.0    KernSmooth_2.23-20    utf8_1.2.1           
 [97] spatstat.geom_3.0-3   plotly_4.10.0         rmarkdown_2.17       
[100] grid_4.1.0            data.table_1.14.0     digest_0.6.27        
[103] xtable_1.8-4          tidyr_1.1.3           httpuv_1.6.1         
[106] munsell_0.5.0         viridisLite_0.4.0     bslib_0.2.5.1